<?php

namespace app\backend\controller\hrm;

use app\common\controller\Basic;
use app\common\service\hrm\DeptService;

/**
 * 部门
 */
class Dept extends Basic
{
    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new DeptService();
    }

    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json($this->service->_list($data, false));
        }
        return _jerr(1000);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            return _jok();
        }
        return _jerr(1000);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $id = input('id/d', 0);
            $data = $this->service->getInfoById($id);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 添加编辑-表单数据处理
     * @param array $data
     */
    protected function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $data['status'] = checkon('status', $data);
            if (isset($data['id']) && isset($data['pid'])) {
                if ($data['id'] == $data['pid']) {
                    $data['pid'] = 0;
                }
            }
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = input('id/d', 0);
            if ($this->service->delete($id)) {
                return _jok(1025);
            };
            return _jerr(1026);
        }
        return _jerr(1000);
    }

    /**
     * 启/禁用
     */
    public function setdisabled()
    {
        if ($this->request->isPost()) {
            $id = input('id/d', 0);
            $field = input('field', '');
            $value = input('value/d', 0);
            if ($this->service->updateField($id,$field,$value)) {
                return _jok(1031);
            };
            return _jerr(1032);
        }
        return _jerr(1000);
    }
}
